<template>
  <div>
    <el-form ref="pram" :model="pram" :rules="rules" label-width="120px" @submit.native.prevent>
      <el-form-item label="训练项目" prop="name" class="dislabel">
        <el-input v-model="pram.name" maxlength="100" placeholder="训练项目"/>
      </el-form-item>
      <el-form-item label="及格标准" prop="standard" class="dislabel">
        <el-input v-model="pram.standard" maxlength="100" placeholder="及格标准"/>
      </el-form-item>
      <el-form-item label="表现" prop="description" class="dislabel">
        <el-input v-model="pram.description" type="textarea"  maxlength="250" :rows="3" placeholder="请输入表现" />
      </el-form-item>
      <el-form-item label="评级" prop="score" class="dislabel">
        <el-select v-model="pram.score" placeholder="评级" clearable style="width: 100%">
          <el-option
            v-for="(item,index) in scoreList"
            :key="index"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handlerSubmit('pram')" v-hasPermi="['admin:person:update','admin:person:save']">{{ isCreate===0?'确定':'更新' }}</el-button>
        <el-button @click="close">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {Debounce} from '@/utils/validate'
export default {
  // name: "edit"
  components: { },
  props: {
    isCreate: {
      type: Number,
      required: true
    },
    editData: {
      type: Object,
      default: () => {
        return { rules: [] }
      }
    },
    editKey: {
      type: Number,
      required: true,
      default: -1,
    },
    allScoreData:{
      type: Array
    },
  },
  data() {
    return {
      constants:this.$constants,
      pram: {
        name: null,
        standard: null,
        description:null,
        score:null,

        scoreTetx:'',
      },
      rules: {
        name: [{ required: true, message: '请填训练项目', trigger: ['blur', 'change'] }],
        description: [{ required: true, message: '请填表现', trigger: ['blur', 'change'] }],
        standard: [{ required: true, message: '请填及格标准', trigger: ['blur', 'change'] }],
        score: [{ required: true, message: '请选择评级', trigger: ['blur', 'change'] }],
      },

      scoreList:[],
    }
  },
  mounted() {
    this.initEditData()
  },
  methods: {
    close() {
      this.$emit('hideTrainDialog')
    },

    initEditData() {
      this.scoreList = this.allScoreData

      if (this.isCreate !== 1) return //编辑
      const { name, standard, description, score} = this.editData //通过list中slot-scope=“scope“，scope.$index就是该行的下标，scope.row就是该行的数据所有消息对象，有了这两个参数就可以实现编辑、删除功能（this.editData就是scope.row传入的对象参数）
      this.pram.name = name
      this.pram.standard = standard
      this.pram.score = score
      this.pram.description = description
    },
    handlerSubmit:Debounce(function(form) {
      this.$refs[form].validate(valid => {
        if (!valid) return
        if (this.isCreate === 0) {
          this.handlerSave()
        } else {
          this.handlerEdit()
        }
      })
    }),
    handlerSave() {
      const scoreData = this.scoreList
      scoreData.forEach((item)=>{
        if(item.id === this.pram.score) this.pram.scoreText = item.name
      })

      this.$emit('assignTrainSaveAndHideTrainDialog',this.pram)
    },
    handlerEdit() {
      const scoreData = this.scoreList
      scoreData.forEach((item)=>{
        if(item.id === this.pram.score) this.pram.scoreText = item.name
      })

      this.$emit('assignTrainEditAndHideTrainDialog',this.pram,this.editKey)
    },
  }
}
</script>

<style scoped lang="scss">
  .dislabel{
    ::v-deep.el-input--small .el-input__inner,
    ::v-deep.el-input--medium .el-input__inner{
      height:36px;
      line-height:36px;
      font-size:12px;
    }
    ::v-deep.el-textarea__inner{
      font-size:12px;
    }

  }
</style>
